/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package simplemetrosystem.view.menu;

import java.util.Observable;
import java.util.Observer;
import javax.swing.JMenu;
import simplemetrosystem.control.EditCommandManager;
import simplemetrosystem.model.charlanguage.NameMap;
import simplemetrosystem.view.MetroFrame;
import simplemetrosystem.view.NewMapSubject;

/**
 * 编辑菜单
 * @author Eric Qian
 */
public class EditMenu extends JMenu implements Observer {

    /**
     * 初始化编辑菜单
     * @param frame
     */
    public EditMenu(MetroFrame frame) {
        super();
        name = NameMap.getInstance().getName("Edit");
        this.setText(name);
        this.frame = frame;
        EditCommandManager.getInstance().addObserver(this);
        addItem();
    }

    /*
     * 导入并对菜单进行排版
     */
    private void addItem() {
        this.add(getEditUndoItem());
        this.add(getEditRedoItem());
        this.addSeparator();
        this.addSeparator();
    }

    /**
     * 得到撤销按键
     * @return 撤销按键
     */
    public EditUndoItem getEditUndoItem() {
        if (euItem == null) {
            euItem = new EditUndoItem(frame);
            euItem.setEnabled(EditCommandManager.getInstance().undoEnabled());
        }
        return euItem;
    }

    /**
     * 
     * @return
     */
    public EditRedoItem getEditRedoItem() {
        if (erItem == null) {
            erItem = new EditRedoItem(frame);
            erItem.setEnabled(EditCommandManager.getInstance().redoEnabled());
        }
        return erItem;
    }

    public void update(Observable o, Object arg) {
        if (o instanceof NewMapSubject) {
            EditCommandManager manager = EditCommandManager.getInstance();
            manager.addObserver(this);
            getEditUndoItem().setEnabled(manager.undoEnabled());
            getEditRedoItem().setEnabled(manager.redoEnabled());
        } else if (o instanceof EditCommandManager) {
            EditCommandManager manager = (EditCommandManager) o;
            getEditUndoItem().setEnabled(manager.undoEnabled());
            getEditRedoItem().setEnabled(manager.redoEnabled());
        }
    }
    private MetroFrame frame;
    private String name;
    private EditUndoItem euItem;
    private EditRedoItem erItem;
}
